/*===============================================*/
/*================= RESTAT MS24757 ==============*/
/*============== F.Cumming & P.Hubert ===========*/
/*============ Regional panel analysis ==========*/
/*===============================================*/

drop _all
clear matrix
global DIR "C:\XXX"
cd "${DIR}"

/*======================================================================*/
/*======================= Denton interpolation =========================*/
/*======================================================================*/

forvalues i = 0(1)12 {
use "RESTAT_MS24757_Regional_Cons_data.dta", clear
format %ty date
xtset reg_id date 
drop if reg_id!=`i'
denton total using Reg_`i'_tot2.dta, interp(cons_q) from("RESTAT_MS24757_Agg_Cons_data.dta") generate(reg_tot_c_lq) stock
use Reg_`i'_tot2.dta, clear
drop total
gen reg_id = `i'
save Reg_`i'.dta, replace
erase Reg_`i'_tot2.dta 
}
use Reg_0, clear 
forvalues i = 1(1)12 {
append using Reg_`i'
}
save "RegionalData.dta", replace

forvalues i = 0(1)12 {
erase Reg_`i'.dta
}

/*=============================================================*/
/*======================= Merge Macro =========================*/
/*=============================================================*/

forvalues i = 1(1)12 {
use "RESTAT_MS24757_Agg_Macro_data.dta", clear 
gen reg_id = `i'
order date reg_id
save Macro_Q_`i'.dta
}
use Macro_Q_1, clear 
forvalues i = 2(1)12 {
append using Macro_Q_`i'
}
save Agg_Macro_Q_Final.dta, replace
forvalues i = 1(1)12 {
erase Macro_Q_`i'.dta
}

use "RegionalData.dta", clear 
merge 1:1 reg_id dateq using Agg_Macro_Q_Final.dta
drop if _merge==1
drop if _merge==2
drop _merge
xtset reg_id dateq
erase Agg_Macro_Q_Final.dta
save "RegionalData.dta", replace

/*=============================================================*/
/*=======================  Merge LTI  =========================*/
/*=============================================================*/

use "RESTAT_MS24757_Regional_LTI_data.dta", clear
by reg_id: gen dateq = tq(2005q2) + _n-1
format %tq dateq
order dateq
xtset reg_id dateq
save Regional_Collapse_Temp.dta, replace

use "RegionalData.dta", clear 
merge 1:1 reg_id dateq using Regional_Collapse_Temp.dta
drop if _merge==1
drop if _merge==2
drop _merge

rename reg_tot_c_lq tot_c

order dateq reg_id tot_c lti*
sort reg_id dateq
xtset reg_id dateq

erase Regional_Collapse_Temp.dta
save "RegionalData.dta", replace

/*======================================================================*/
/*============= CROSS-SECTIONAL & TIME-SERIES VARIATION ================*/
/*======================================================================*/

use "RegionalData.dta", clear 

//--- Time-series & Cross-sectional variation
sum lti_adj_95

//--- for one region across dates: TS
cap drop sd_ts sd_reg*
gen sd_ts = .
forvalues i = 1(1)12 {
egen sd_reg`i' = sd(lti_adj_95) if reg_id==`i'
replace sd_ts = sd_reg`i' if reg_id==`i'
}
sum sd_ts
sum sd_ts if dateq==tq(2009q1)

//--- for one date across reg_id: CS
cap drop daten sd_cs sd_date*
gen daten = .
replace daten = 1 if dateq==tq(2009q1) 
replace daten = 1+daten[_n-1] if dateq>tq(2009q1) & reg_id==reg_id[_n-1]
gen sd_cs = .
forvalues i = 1(1)32 {
egen sd_date`i' = sd(lti_adj_95) if daten==`i'
replace sd_cs = sd_date`i' if daten==`i'
}
sum sd_cs
sum sd_cs if reg_id==1

/*======================================================================*/
/*========================= MAIN ESTIMATION ============================*/
/*======================================================================*/

use "RegionalData.dta", clear 

xtset reg_id dateq

//--- Housekeeping, Labels and Axis
local variable tot_c
foreach var of local variable {
replace `var' = ln(`var')*100
} 
label variable lti_adj_95 "LTI>p(95)"
label variable tot_c "Total consumption"
gen zero = 0
gen xaxis = 0 if _n==1
replace xaxis = xaxis[_n-1]+1 if _n>1
replace xaxis = . if _n>8
gen year = year(dofq(dateq))
tab year, gen(year_)

//----- Smoothing 
cap drop s_lti_*
local variable lti_adj_95
foreach var of local variable {
tssmooth ma s_`var' = `var', window(2 1 2) replace
label variable s_`var'
}

//----- Regional controls
sum hp pc_income LTI interest mort_dur remortg

//------ LP ------// 
cap drop h_*r2y* l_*r2y*
cap drop m_*r2y*
local cyclevar s
local statevar lti_adj_95
local variable tot_c
local forward F0 F1 F2 F3 F4
foreach cyvar of local cyclevar {
foreach stvar of local statevar {
foreach var of local variable {
foreach lead of local forward {
quiet sum `cyvar'_`stvar'
global m  = r(mean)
global sd = r(sd)
local mpshock r_2y_shsum
local controls L1.S1.`var' S4.hp pc_income LTI interest mort_dur remortg
quiet xtreg `lead'.S1.`var' c.`cyvar'_`stvar'##c.`mpshock' c.`cyvar'_`stvar'##c.(`controls') year_*, fe
global high "`mpshock' + ($m+1*$sd)*c.`cyvar'_`stvar'#c.`mpshock'"
global low "`mpshock' + ($m-1*$sd)*c.`cyvar'_`stvar'#c.`mpshock'"
global mean "`mpshock' + ($m)*c.`cyvar'_`stvar'#c.`mpshock'"
quiet lincom $high
scalar p_bet_`lead'_`var'_`stvar' = r(estimate)
scalar p_se_`lead'_`var'_`stvar' = r(se)
scalar p_a1_`lead'_`var'_`stvar' = p_bet_`lead'_`var'_`stvar' + 1*p_se_`lead'_`var'_`stvar'
scalar p_b1_`lead'_`var'_`stvar' = p_bet_`lead'_`var'_`stvar' - 1*p_se_`lead'_`var'_`stvar'
scalar p_a2_`lead'_`var'_`stvar' = p_bet_`lead'_`var'_`stvar' + 1.64*p_se_`lead'_`var'_`stvar'
scalar p_b2_`lead'_`var'_`stvar' = p_bet_`lead'_`var'_`stvar' - 1.64*p_se_`lead'_`var'_`stvar'
matrix alpha11=nullmat(alpha11), p_a2_`lead'_`var'_`stvar'
matrix alpha12=nullmat(alpha12), p_a1_`lead'_`var'_`stvar'
matrix alpha13=nullmat(alpha13), p_bet_`lead'_`var'_`stvar'
matrix alpha14=nullmat(alpha14), p_b1_`lead'_`var'_`stvar'
matrix alpha15=nullmat(alpha15), p_b2_`lead'_`var'_`stvar'
matrix alpha16=alpha11\alpha12\alpha13\alpha14\alpha15
scalar drop _all
quiet lincom $low
scalar p_bet_`lead'_`var'_`stvar' = r(estimate)
scalar p_se_`lead'_`var'_`stvar' = r(se)
scalar p_a1_`lead'_`var'_`stvar' = p_bet_`lead'_`var'_`stvar' + 1*p_se_`lead'_`var'_`stvar'
scalar p_b1_`lead'_`var'_`stvar' = p_bet_`lead'_`var'_`stvar' - 1*p_se_`lead'_`var'_`stvar'
scalar p_a2_`lead'_`var'_`stvar' = p_bet_`lead'_`var'_`stvar' + 1.64*p_se_`lead'_`var'_`stvar'
scalar p_b2_`lead'_`var'_`stvar' = p_bet_`lead'_`var'_`stvar' - 1.64*p_se_`lead'_`var'_`stvar'
matrix alpha21=nullmat(alpha21), p_a2_`lead'_`var'_`stvar'
matrix alpha22=nullmat(alpha22), p_a1_`lead'_`var'_`stvar'
matrix alpha23=nullmat(alpha23), p_bet_`lead'_`var'_`stvar'
matrix alpha24=nullmat(alpha24), p_b1_`lead'_`var'_`stvar'
matrix alpha25=nullmat(alpha25), p_b2_`lead'_`var'_`stvar'
matrix alpha26=alpha21\alpha22\alpha23\alpha24\alpha25
scalar drop _all
quiet lincom $mean
scalar p_bet_`lead'_`var'_`stvar' = r(estimate)
scalar p_se_`lead'_`var'_`stvar' = r(se)
scalar p_a1_`lead'_`var'_`stvar' = p_bet_`lead'_`var'_`stvar' + 1*p_se_`lead'_`var'_`stvar'
scalar p_b1_`lead'_`var'_`stvar' = p_bet_`lead'_`var'_`stvar' - 1*p_se_`lead'_`var'_`stvar'
scalar p_a2_`lead'_`var'_`stvar' = p_bet_`lead'_`var'_`stvar' + 1.64*p_se_`lead'_`var'_`stvar'
scalar p_b2_`lead'_`var'_`stvar' = p_bet_`lead'_`var'_`stvar' - 1.64*p_se_`lead'_`var'_`stvar'
matrix alpha31=nullmat(alpha31), p_a2_`lead'_`var'_`stvar'
matrix alpha32=nullmat(alpha32), p_a1_`lead'_`var'_`stvar'
matrix alpha33=nullmat(alpha33), p_bet_`lead'_`var'_`stvar'
matrix alpha34=nullmat(alpha34), p_b1_`lead'_`var'_`stvar'
matrix alpha35=nullmat(alpha35), p_b2_`lead'_`var'_`stvar'
matrix alpha36=alpha31\alpha32\alpha33\alpha34\alpha35
scalar drop _all
}
quietly matrix alpha16=alpha16'
quietly matrix alpha26=alpha26'
quietly matrix alpha36=alpha36'
quietly svmat alpha16, names(h_`cyvar'_`stvar'_r2y_`var'_)
quietly svmat alpha26, names(l_`cyvar'_`stvar'_r2y_`var'_)
quietly svmat alpha36, names(m_`cyvar'_`stvar'_r2y_`var'_)
quietly mat list alpha16
quietly mat list alpha26
quietly mat list alpha36
quietly mat drop _all
}
}
} 
//GRAPH
local cyclevar s
local statevar lti_adj_95
local variable tot_c 
foreach cyvar of local cyclevar {
foreach stvar of local statevar {
foreach var of local variable {
local vtext : variable label `var' 
if `"`vtext'"' == "" local vtext "`v'" 
local v2text : variable label `stvar' 
if `"`v2text'"' == "" local vtext "`v'"
set graphics off
twoway (line zero xaxis, lcolor(black) lwidth(thin)) ///
(rarea m_`cyvar'_`stvar'_r2y_`var'_1 m_`cyvar'_`stvar'_r2y_`var'_5 xaxis, fcolor(gray) fintensity(25) lcolor(gray) lwidth(none)) ///
(rarea m_`cyvar'_`stvar'_r2y_`var'_2 m_`cyvar'_`stvar'_r2y_`var'_4 xaxis, fcolor(gray) fintensity(50) lcolor(gray) lwidth(none)) ///
(connected m_`cyvar'_`stvar'_r2y_`var'_3 xaxis, lcolor(black) mcolor(black) msymbol(circle) msize(large) mfcolor(none)) if _n<6, ///
ylabel(-8(4)4, angle(horizontal) labsize(medium) axis(1)) ytitle("Per cent", axis(1)) xtitle("") ///
xlabel(0(1)4, labsize(medium)) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
plotregion(fcolor(white) lcolor(black) ifcolor(white) ilcolor(white)) name(average, replace) ///
title(`"`vtext'"', size(medlarge) color(black)) legend(cols(1) order(4 "Average effect") ///
size(medsmall) symysize(*.4) symxsize(*.4) rowgap(*.1) colgap(*.1) ring(0) position(11)) aspectratio(0.9) 
}
}
}
local cyclevar s
local statevar lti_adj_95
local variable tot_c 
foreach cyvar of local cyclevar {
foreach stvar of local statevar {
foreach var of local variable {
local vtext : variable label `var' 
if `"`vtext'"' == "" local vtext "`v'" 
local v2text : variable label `stvar' 
if `"`v2text'"' == "" local vtext "`v'"
set graphics off
twoway (line zero xaxis, lcolor(black) lwidth(thin)) ///
(rarea h_`cyvar'_`stvar'_r2y_`var'_1 h_`cyvar'_`stvar'_r2y_`var'_5 xaxis, fcolor(gray) fintensity(25) lcolor(gray) lwidth(none)) ///
(rarea h_`cyvar'_`stvar'_r2y_`var'_2 h_`cyvar'_`stvar'_r2y_`var'_4 xaxis, fcolor(gray) fintensity(50) lcolor(gray) lwidth(none)) ///
(connected h_`cyvar'_`stvar'_r2y_`var'_3 xaxis, lcolor(black) mcolor(black) msymbol(circle) msize(large) mfcolor(none)) ///
(rarea l_`cyvar'_`stvar'_r2y_`var'_1 l_`cyvar'_`stvar'_r2y_`var'_5 xaxis, fcolor(gray) fintensity(75) lcolor(navy) lwidth(none)) ///
(rarea l_`cyvar'_`stvar'_r2y_`var'_2 l_`cyvar'_`stvar'_r2y_`var'_4 xaxis, fcolor(gray) fintensity(100) lcolor(navy) lwidth(none)) ///
(connected l_`cyvar'_`stvar'_r2y_`var'_3 xaxis, lcolor(black) mcolor(black) msymbol(circle) msize(large) mfcolor(black)) if _n<6, ///
ylabel(-8(4)4, angle(horizontal) labsize(medium) axis(1)) ytitle("Per cent", axis(1)) xtitle("") ///
xlabel(0(1)4, labsize(medium)) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
plotregion(fcolor(white) lcolor(black) ifcolor(white) ilcolor(white)) name(`cyvar'_`stvar'_`var', replace) ///
title(`"`vtext'"', size(medlarge) color(black)) legend(cols(1) order(4 "High S{subscript:t,r}" 7 "Low S{subscript:t,r}") ///
size(medsmall) symysize(*.4) symxsize(*.4) rowgap(*.1) colgap(*.1) ring(0) position(7)) aspectratio(0.9) 
}
}
}
set graphics on
graph combine average s_lti_adj_95_tot_c, ///
rows(1) cols(2) ycommon imargin(tiny) graphregion(margin(tiny) fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
plotregion(margin(tiny) fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) iscale(*1.3) xsize(2) ysize(1) 
graph export Fig3.png, width(2000) height(1000) replace
graph export Fig3.eps, replace

